package com.zhf.service;

import com.zhf.bean.Student;
import com.zhf.dao.StudentDao;
import com.zhf.factory.StudentDaoFactory;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/10/17 16:12
 */
public class StudentService {
    private StudentDao studentDao;

    public StudentService() {
        this.studentDao = StudentDaoFactory.getStudentDao();
    }

    /**
     * 查询该学号的学生是否存在
     * @param id
     * @return
     */
    public boolean isStudentID(String id) {
        try {
            Student studentByID = studentDao.getStudentByID(id);
            if (studentByID == null) {
                return false;
            } else {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 通过学号查找学生
     * @param id
     * @return
     */
    public Student getStudentByID(String id){
        try {
            Student student = studentDao.getStudentByID(id);
            return student;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取学生列表
     * @return
     */
    public List<Student> getStudents(){
        try {
            List<Student> students = studentDao.getStudents();
            return students;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除学生
     * @param id
     * @return
     */
    public boolean deletStudent(String id){
        try {
            int i = studentDao.deleteStudentByID(id);
            if (i>0){
                return true;
            }else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 添加学生
     * @param student
     * @return
     */
    public boolean addStudent(Student student){
        try {
            int i = studentDao.addStudent(student);
            if (i>0){
                return true;
            }else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 修改学生信息
     * @param student
     * @return
     */
    public boolean updateStudent(Student student){
        try {
            int i = studentDao.updateStudent(student);
            if (i>0){
                return true;
            }else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 检查该班级是否有学生
     * @param className
     * @return
     */
    public boolean isStudentNull(String className){
        try {
            int i = studentDao.isStudentInClass(className);
            if (i>0){
                //有学生
                return true;
            }else {
                //无学生
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  true;
    }
}
